# coding=utf-8

import json
import os
import random


class _AreaAbstract(object):
    def __init__(self):
        self.display_name = None
        self.name = None
        self.id = 0

    def __unicode__(self):
        return self.name


class Province(_AreaAbstract):
    def __init__(self):
        super(Province, self).__init__()
        self.cities = []
        self.is_municipality = False


class City(_AreaAbstract):
    def __init__(self):
        super(City, self).__init__()
        self.areas = []
        self.province = None


class Area(_AreaAbstract):
    def __init__(self):
        super(Area, self).__init__()
        self.city = None


def _all_provinces():
    provinces = []

    BASE_DIR = os.path.dirname(__file__)
    file_path = os.path.join(BASE_DIR, 'city_code.json')
    s = json.load(open(file_path))

    for dic in s:
        p = Province()
        p.name = dic['name']
        p.display_name = dic['display_name']
        p.id = int(dic['id'])
        p.is_municipality = dic.get('is_municipality') or False
        provinces.append(p)

        cities = []
        p.cities = cities

        for dic_city in dic['cities']:
            c = City()
            c.name = dic_city['name']
            c.display_name = dic_city['display_name']
            c.id = int(dic_city['id'])
            c.province = p

            cities.append(c)

            areas = []
            c.areas = areas

            for area_dic in dic_city['areas']:
                a = Area()
                a.name = area_dic['name']
                a.display_name = area_dic['display_name']
                a.id = int(area_dic['id'])
                a.city = c
                areas.append(a)

    return provinces


all_provinces = _all_provinces()


def _all_places():
    places = {}
    for p in all_provinces:
        places[p.id] = p

        for c in p.cities:
            places[c.id] = c

            for a in c.areas:
                places[a.id] = a

    return places


all_places = _all_places()


def rand_city_code():
    return random.choice(list(all_places.keys()))


def province_code_of_city(city_code):
    int_code = int(city_code)
    if int_code > 650000:
        return 0
    return (int_code / 10000) * 10000


def province_range_in(city_code):
    min = city_code / 10000 * 10000
    max = min + 10000
    return range(min, max)


def is_code_province(city_code):
    return city_code % 10000 == 0


def is_code_city(city_code):
    return city_code % 100 == 0


def province_code_of_code(city_code):
    pCode = int(int(city_code / 10000) * 10000)
    return pCode


def is_code_municipality(city_code):
    pCode = province_code_of_city(city_code)
    if pCode in [110000, 120000, 310000, 500000]:
        return True
    return False


def city_code_of_code(code):
    ccode = int(int(code / 1000) * 1000)

    if not is_code_province(ccode) and all_places.get(ccode):
        return ccode

    ccode2 = int(code / 100)
    ccode2 = int(ccode2 * 100)
    return ccode2


def display_place_of_code(city_code):
    city = all_places.get(city_code, None)
    print(f'>>>>>>>>>>>>>>>>>{city}')
    if city:
        return city.name
    return ''
